﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace MainForm
{
    public partial class StartForm : Form
    {
        Options options = null;
        bool beginMove = false;//初始化鼠标位置
        int currentXPosition;
        int currentYPosition;
        Graphics DrawTextGraphics = null;
        public StartForm(Options options)
        {
            InitializeComponent();
            this.options = options;
        }

        private void StartForm_Load(object sender, EventArgs e)
        {
            Win32API.RegisterHotKey(this.Handle, 100, 0, (int)this.options.StartKey);
        }

        private void StartForm_Shown(object sender, EventArgs e)
        {
            this.TransparencyKey = Color.Black;
            DrawTextGraphics = this.CreateGraphics();
            var hWnd = this.Handle;
            Win32API.SetWindowPos(this.Handle, Win32API.HWND_TOPMOST, 0, 0, 0, 0, Win32API.SWP_NOSIZE | Win32API.SWP_NOMOVE);
            var str = string.Format("已就绪，按{0}开始计时,右键双击退出", this.options.StartKey);
            var font = new Font("微软雅黑", 13f);
            var brush = new SolidBrush(Color.Red);
            var textSize = DrawTextGraphics.MeasureString(str, font, this.ClientSize.Width);
            var x = (this.ClientSize.Width - textSize.Width) / 2;
            var y = (this.ClientSize.Height - textSize.Height) / 2;
            DrawTextGraphics.DrawString(str, font, brush, x, y);

            font = new Font("微软雅黑", 10f);
            y = y + textSize.Height + 5;
            str = "拖动窗口可改变位置";
            textSize = DrawTextGraphics.MeasureString(str, font, this.ClientSize.Width);
            x = (this.ClientSize.Width - textSize.Width) / 2;
            DrawTextGraphics.DrawString(str, font, brush, x, y);
            font.Dispose();
            brush.Dispose();

            this.countdowner.ChangeColor(this.options.BackColor, this.options.BarColor, this.options.TextColor);
            this.countdowner.End += (s, args) =>
            {
                this.Invoke(new Action(() =>
                {
                    this.countdowner.Hide();
                    str = string.Format("已执行完毕，按{0}重新开始,ESC退出", this.options.StartKey);
                    font = new Font("微软雅黑", 12f);
                    brush = new SolidBrush(Color.Red);
                    textSize = DrawTextGraphics.MeasureString(str, font, this.ClientSize.Width);
                    x = (this.ClientSize.Width - textSize.Width) / 2;
                    y = (this.ClientSize.Height - textSize.Height) / 2;
                    DrawTextGraphics.DrawString(str, font, brush, x, y);
                }));
            };
            this.countdowner.MouseDown += this.StartForm_MouseDown;
            this.countdowner.MouseUp += this.StartForm_MouseUp;
            this.countdowner.MouseMove += this.StartForm_MouseMove;
            this.countdowner.MouseDoubleClick += this.StartForm_MouseDoubleClick;
        }

        private void StartForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                beginMove = true;
                currentXPosition = MousePosition.X;
                currentYPosition = MousePosition.Y;
            }
        }

        private void StartForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentXPosition = 0;
                currentYPosition = 0;
                beginMove = false;
            }
        }

        private void StartForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (beginMove)
            {
                this.Left += MousePosition.X - currentXPosition;
                this.Top += MousePosition.Y - currentYPosition;
                currentXPosition = MousePosition.X;
                currentYPosition = MousePosition.Y;
            }
        }

        private void StartForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Win32API.UnregisterHotKey(this.Handle, 100);
            DrawTextGraphics.Dispose();
            this.countdowner.StopAll();
            this.countdowner.Dispose();
        }
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    {
                        if (m.WParam.ToInt32() == 100)
                        {
                            this.Start();
                        }
                        else if (m.WParam.ToInt32() == 101)
                        {
                            this.Close();
                        }
                        break;
                    }
            }
            base.WndProc(ref m);
        }
        void Start()
        {
            DrawTextGraphics.Clear(Color.FromArgb(240, 240, 240));
            this.countdowner.Show();
            this.countdowner.Start(this.options.GameEvents.Events);
        }

        private void StartForm_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.Close();
            }
        }
    }
}
